package com.kxmall.web.controller.huishouapp;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.enums.BillDetailEnum;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.huishou.domain.bo.HsWithdrawRecordBo;
import com.kxmall.huishou.domain.vo.HsWithdrawRecordVo;
import com.kxmall.huishou.service.IHsWithdrawRecordService;
import com.kxmall.user.domain.bo.KxUserBillBo;
import com.kxmall.user.domain.bo.KxUserBo;
import com.kxmall.user.domain.vo.KxUserVo;
import com.kxmall.user.service.IUserBillService;
import com.kxmall.web.controller.user.service.IKxAppUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户提现记录
 *
 * @author lyrics
 * @date 2023-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/withdrawRecord")
public class HsAppWithdrawRecordController extends BaseAppController {

    private final IHsWithdrawRecordService iHsWithdrawRecordService;

    private final IKxAppUserService userService;

    private final IUserBillService iUserBillService;

    /**
     * 查询用户提现记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<HsWithdrawRecordVo> list(HsWithdrawRecordBo bo, PageQuery pageQuery) {
        if(bo == null){
            bo = new HsWithdrawRecordBo();
        }
        bo.setUserId(getAppLoginUser().getUserId());
        return iHsWithdrawRecordService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取用户提现记录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<HsWithdrawRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        HsWithdrawRecordVo hsWithdrawRecordVo = iHsWithdrawRecordService.queryById(id);
        if(hsWithdrawRecordVo == null || hsWithdrawRecordVo.getUserId().longValue() != getLoginUser().getUserId().longValue()){
            return R.fail("数据不存在！");
        }
        return R.ok(hsWithdrawRecordVo);
    }

    /**
     * 新增用户提现记录
     */
    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsWithdrawRecordBo bo) {

        Long userId = getAppLoginUser().getUserId();
        String username = getUsername();
        bo.setUsername(username);
        bo.setUserId(userId);
        if(bo.getAmount().compareTo(BigDecimal.ZERO) <= 0){
            return R.fail("提现金额错误！");
        }
        if(bo.getAccountType() >3 || bo.getAccountType() < 1){
            return R.fail("提现账户类型错误！");
        }
        if(bo.getAccountType() == 3){
            if(bo.getBankSubName() == null || bo.getBankName() == null){
                return R.fail("银行卡信息不全！");
            }
        }
        bo.setStatus(0);

        //查询用户余额，扣减余额
        KxUserVo kxUserVo = userService.queryById(userId);
        if(kxUserVo == null){
            return R.fail("提现用户不存在！");
        }
        if(kxUserVo.getNowMoney().compareTo(bo.getAmount()) < 0){
            return R.fail("提现余额不足！");
        }

        KxUserBo kxUserBo = new KxUserBo();
        kxUserBo.setNowMoney(kxUserVo.getNowMoney().subtract(bo.getAmount()));
        kxUserBo.setUid(userId);
        userService.updateByBo(kxUserBo);

        boolean flag = iHsWithdrawRecordService.insertByBo(bo);
        if(flag){
            //生成账户流水
            KxUserBillBo kxUserBillBo = new KxUserBillBo();
            kxUserBillBo.setIsDel(0);
            kxUserBillBo.setUid(userId);
            kxUserBillBo.setLinkId(bo.getId()+"");
            kxUserBillBo.setPm(0);
            kxUserBillBo.setTitle("发起提现"+bo.getAmount());
            kxUserBillBo.setCategory(BillDetailEnum.CATEGORY_1.getValue());
            kxUserBillBo.setType(BillDetailEnum.TYPE_4.getValue());
            kxUserBillBo.setNumber(bo.getAmount());
            kxUserBillBo.setBalance(kxUserBo.getNowMoney());
            kxUserBillBo.setStatus(1);

            iUserBillService.insertByBo(kxUserBillBo);
        }

        return toAjax(flag);
    }




}
